package com.banciyuan.bcywebview.biz.circles.smooth.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.biz.detail.post.PostDetailActivity;
import com.banciyuan.bcywebview.biz.groupdetail.AddGroupActivity;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.GroupWork;
import de.greenrobot.daoexample.model.Multi;
import de.greenrobot.daoexample.model.Post;

/**
 * Created by squall on 2016/9/30.
 */

public class CircleSmoothTalkAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<GroupWork> glist;
    private Context mContext;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;

    private boolean go_detail;
    private boolean loading;
    private boolean is_end;
    private int height;
    private int min_height;
    private int max = -1;

    private String type;
    private String worktitle;

    private String count;


    public CircleSmoothTalkAdapter(List<GroupWork> glist, Context mContext, boolean loading, String type, String worktitle, boolean go_detail) {
        this.glist = glist;
        this.type = type;
        this.worktitle = worktitle;
        this.loading = loading;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.imageLoader = ImageLoader.getInstance();
        this.min_height = CommonUtils.getHeightPixels((Activity) mContext) - CommonUtils.getStatusPixels((Activity) mContext) - UIUtils.dip2px(94, mContext);
        this.go_detail = go_detail;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return new BlankHolder(inflater.inflate(R.layout.smooth_blank, null));
            case 1:
                return new BottomHolder(inflater.inflate(R.layout.circle_topic_smooth_bottom, null));
            case 2:
                return new GroupViewNewHolder(inflater.inflate(R.layout.topic_item, null));
            case 3:
                return new GroupHeader(inflater.inflate(R.layout.circle_talk_header_item, null));
            default:
                return new BlankHolder(inflater.inflate(R.layout.smooth_blank, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                setHeaderBlankHolder((BlankHolder) holder);
                break;
            case 1:
                setFooterBlankHolder(height, (BottomHolder) holder);
                break;
            case 2:
                setTalkItem(position - 2, (GroupViewNewHolder) holder);
                setHeight(holder, position - 2);
                break;
            case 3:
                setGroupHeader((GroupHeader) holder);
                setHeight(holder, position - 2);
                break;
        }
    }

    private void setHeight(RecyclerView.ViewHolder viewHolder, int position) {
        if (height < min_height && position > max) {
            viewHolder.itemView.measure(
                    View.MeasureSpec.makeMeasureSpec(CommonUtils.getWidthPixels((Activity) mContext), View.MeasureSpec.AT_MOST),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            height += viewHolder.itemView.getMeasuredHeight();
            max = position;
        }
    }

    private void setGroupHeader(GroupHeader groupHeader) {
        if (!TextUtils.isEmpty(count)) {
            RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
            groupHeader.itemView.setLayoutParams(layoutParams);
            groupHeader.talk_num.setText(String.format(mContext.getString(R.string.post_num_format), count));
        }
    }

    private void setTalkItem(int i, GroupViewNewHolder groupViewNewHolder) {
        final GroupWork groupWork = glist.get(i);
        if (groupWork != null) {
            int post_num = getIntegerPostNum(groupWork);
            if (post_num > 2) {
                groupViewNewHolder.replyCount.setVisibility(View.VISIBLE);
                groupViewNewHolder.replyCount.setText(String.format(mContext.getString(R.string.total_answer_count_format), post_num));
            } else {
                groupViewNewHolder.replyCount.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(groupWork.getName())) {
                groupViewNewHolder.groupTitle.setText(Html.fromHtml(groupWork.getName()));
            }
            List<Post> posts = groupWork.getPosts();
            switch (posts.size()) {
                case 0:
                    groupViewNewHolder.topicOne.setVisibility(View.GONE);
                    groupViewNewHolder.topicTwo.setVisibility(View.GONE);
                    groupViewNewHolder.nodataView.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    groupViewNewHolder.topicOne.setVisibility(View.VISIBLE);
                    groupViewNewHolder.topicTwo.setVisibility(View.GONE);
                    groupViewNewHolder.nodataView.setVisibility(View.GONE);
                    processGroupViewOne(groupViewNewHolder, posts, groupWork);
                    break;
                default:
                    groupViewNewHolder.topicOne.setVisibility(View.VISIBLE);
                    groupViewNewHolder.topicTwo.setVisibility(View.VISIBLE);
                    groupViewNewHolder.nodataView.setVisibility(View.GONE);
                    processGroupViewOne(groupViewNewHolder, posts, groupWork);
                    processGroupViewTwo(groupViewNewHolder, posts, groupWork);
                    break;
            }
            groupViewNewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, groupWork.getGid(), groupWork.getName(), "", NewTeamDetailActivity.TEAMDETAIL_TIME);
                }
            });
        }
    }

    private int getIntegerPostNum(GroupWork groupWork) {
        try {
            return Integer.valueOf(groupWork.getPost_num());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private void setFooterBlankHolder(int height, BottomHolder bottomHolder) {
        int footer = min_height - height;
        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, footer > 0 ? footer : 1);
        bottomHolder.blank.setLayoutParams(layoutParams);
        if (loading) {
            bottomHolder.loading.setVisibility(View.VISIBLE);
        } else {
            bottomHolder.loading.setVisibility(View.GONE);
        }
        if (is_end) {
            bottomHolder.empty.setVisibility(View.VISIBLE);
            if (getItemCount() == 2) {
                bottomHolder.empty_pic.setVisibility(View.VISIBLE);
                bottomHolder.empty_info.setText(mContext.getString(R.string.group_emptydesc));
            } else {
                if (footer < UIUtils.dip2px(120, mContext)) {
                    RecyclerView.LayoutParams bottom = new RecyclerView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    bottomHolder.blank.setLayoutParams(bottom);
                }
                bottomHolder.empty_pic.setVisibility(View.GONE);
                bottomHolder.empty_info.setText(mContext.getString(R.string.hotworksbottomtext));
            }
        } else {
            bottomHolder.empty.setVisibility(View.GONE);
        }
        bottomHolder.empty_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AddGroupActivity.class);
                if (type.equals("work")) {
                    intent.putExtra(gotoUtil.INTENT_VALUE_TWO, worktitle);
                } else if (type.equals("tag")) {
                    intent.putExtra(gotoUtil.INTENT_VALUE_THREE, worktitle);
                }
                mContext.startActivity(intent);
            }
        });
    }

    private void setHeaderBlankHolder(BlankHolder blankHolder) {
        int offset = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            offset = UIUtils.dip2px(20, mContext);
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(212, mContext)+offset);
        blankHolder.blank.setLayoutParams(layoutParams);
    }

    @Override
    public int getItemCount() {
        return glist.size() + 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (position == 1) {
            return 3;
        } else if (position == getItemCount() - 1) {
            return 1;
        } else {
            return 2;
        }
    }

    private class BlankHolder extends RecyclerView.ViewHolder {

        View blank;

        public BlankHolder(View itemView) {
            super(itemView);
            blank = itemView.findViewById(R.id.blank);
        }
    }

    private class BottomHolder extends RecyclerView.ViewHolder {

        RelativeLayout blank;
        RelativeLayout empty;
        LinearLayout loading;
        ImageView empty_pic;
        TextView empty_info;
        TextView empty_click;


        public BottomHolder(View itemView) {
            super(itemView);
            blank = (RelativeLayout) itemView.findViewById(R.id.smooth_bottom);
            empty = (RelativeLayout) itemView.findViewById(R.id.rl_group_empytview);
            loading = (LinearLayout) itemView.findViewById(R.id.progressbar_loading_layout);

            empty_pic = (ImageView) itemView.findViewById(R.id.iv_group_empty_pic);
            empty_info = (TextView) itemView.findViewById(R.id.tv_group_desc);
            empty_click = (TextView) itemView.findViewById(R.id.tv_group_desc_second);
        }
    }

    private class GroupHeader extends RecyclerView.ViewHolder {

        TextView talk_num;

        public GroupHeader(View itemView) {
            super(itemView);
            talk_num = (TextView) itemView.findViewById(R.id.talk_num);
        }
    }


    private class GroupViewNewHolder extends RecyclerView.ViewHolder {
        TextView replyCount;
        TextView groupTitle;
        ImageView headOne;
        ImageView headTwo;
        TextView nameOne;
        TextView nameTwo;
        TextView descOne;
        TextView descTwo;
        View imagesOne;
        View imagesTwo;
        ImageView firstPicOne;
        ImageView firstPicTwo;
        ImageView firstPicThree;
        ImageView sencondPicOne;
        ImageView sencondPicTwo;
        ImageView sencondPicThree;
        View topicOne;
        View topicTwo;
        View nodataView;
        LinearLayout head;
        View split;


        public GroupViewNewHolder(View convertView) {
            super(convertView);
            head = (LinearLayout) convertView.findViewById(R.id.rl_head);
            replyCount = (TextView) convertView.findViewById(R.id.tv_reply_count);
            groupTitle = (TextView) convertView.findViewById(R.id.tv_topic_title);
            headOne = (ImageView) convertView.findViewById(R.id.civ_header_one);
            headTwo = (ImageView) convertView.findViewById(R.id.civ_header_two);
            nameOne = (TextView) convertView.findViewById(R.id.tv_name_one);
            nameTwo = (TextView) convertView.findViewById(R.id.tv_name_two);
            descOne = (TextView) convertView.findViewById(R.id.tv_desc_one);
            descTwo = (TextView) convertView.findViewById(R.id.tv_desc_two);
            imagesOne = convertView.findViewById(R.id.ll_pics_one);
            imagesTwo = convertView.findViewById(R.id.ll_pics_two);
            firstPicOne = (ImageView) convertView.findViewById(R.id.iv_picsone_one);
            firstPicTwo = (ImageView) convertView.findViewById(R.id.iv_picsone_two);
            firstPicThree = (ImageView) convertView.findViewById(R.id.iv_picsone_three);
            sencondPicOne = (ImageView) convertView.findViewById(R.id.iv_picstwo_one);
            sencondPicTwo = (ImageView) convertView.findViewById(R.id.iv_picstwo_two);
            sencondPicThree = (ImageView) convertView.findViewById(R.id.iv_picstwo_three);
            topicOne = convertView.findViewById(R.id.rl_topic_one);
            topicTwo = convertView.findViewById(R.id.rl_topic_two);
            nodataView = convertView.findViewById(R.id.tv_nopost);
        }
    }

    private void processGroupViewOne(GroupViewNewHolder groupViewHolder, List<Post> posts, final GroupWork groupWork) {
        final Post post = posts.get(0);
        if (null == post)
            return;
        imageLoader.displayImage(post.getUavatar(), groupViewHolder.headOne, ImageOptions.optionHead);
        if (!TextUtils.isEmpty(post.getUname())) {
            groupViewHolder.nameOne.setText(Html.fromHtml(post.getUname()));
        }
        if (!TextUtils.isEmpty(post.getPlain())) {
            groupViewHolder.descOne.setText(Html.fromHtml(post.getPlain()));
            groupViewHolder.descOne.setVisibility(View.VISIBLE);
        } else {
            groupViewHolder.descOne.setVisibility(View.GONE);
        }
        List<Multi> multi = post.getMulti();
        if (null == multi || multi.size() == 0) {
            groupViewHolder.imagesOne.setVisibility(View.GONE);
        } else {
            groupViewHolder.imagesOne.setVisibility(View.VISIBLE);
            switch (multi.size()) {
                case 1:
                    imageLoader.displayImage(multi.get(0).getPath(), groupViewHolder.firstPicOne, ImageOptions.options);
                    groupViewHolder.firstPicOne.setVisibility(View.VISIBLE);
                    groupViewHolder.firstPicTwo.setVisibility(View.INVISIBLE);
                    groupViewHolder.firstPicThree.setVisibility(View.INVISIBLE);
                    break;
                case 2:
                    imageLoader.displayImage(multi.get(0).getPath(), groupViewHolder.firstPicOne, ImageOptions.options);
                    imageLoader.displayImage(multi.get(1).getPath(), groupViewHolder.firstPicTwo, ImageOptions.options);
                    groupViewHolder.firstPicOne.setVisibility(View.VISIBLE);
                    groupViewHolder.firstPicTwo.setVisibility(View.VISIBLE);
                    groupViewHolder.firstPicThree.setVisibility(View.INVISIBLE);
                    break;
                default:
                    imageLoader.displayImage(multi.get(0).getPath(), groupViewHolder.firstPicOne, ImageOptions.options);
                    imageLoader.displayImage(multi.get(1).getPath(), groupViewHolder.firstPicTwo, ImageOptions.options);
                    imageLoader.displayImage(multi.get(2).getPath(), groupViewHolder.firstPicThree, ImageOptions.options);
                    groupViewHolder.firstPicOne.setVisibility(View.VISIBLE);
                    groupViewHolder.firstPicTwo.setVisibility(View.VISIBLE);
                    groupViewHolder.firstPicThree.setVisibility(View.VISIBLE);
                    break;
            }
        }

        if (!TextUtils.isEmpty(post.getUid())) {
            View.OnClickListener onClick = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, post.getUid());
                }
            };
            groupViewHolder.headOne.setOnClickListener(onClick);
            groupViewHolder.nameOne.setOnClickListener(onClick);
        }
        if (go_detail) {
            if (!TextUtils.isEmpty(post.getPost_id())) {
                View.OnClickListener onClick = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, PostDetailActivity.class);
                        intent.putExtra(MyConstants.POSTIDVALUE, post.getPost_id());
                        intent.putExtra(MyConstants.GIDVALUE, groupWork.getGid());
                        intent.putExtra(MyConstants.TIMELINETYPE, TimelineFactory.TIMELINE_TYPE_GROUP);
                        mContext.startActivity(intent);
                    }
                };

                groupViewHolder.descOne.setOnClickListener(onClick);
                groupViewHolder.imagesOne.setOnClickListener(onClick);
            }
        }
    }

    private void processGroupViewTwo(GroupViewNewHolder groupViewHolder, List<Post> posts, final GroupWork groupWork) {
        final Post post = posts.get(1);
        if (null == post)
            return;
        imageLoader.displayImage(post.getUavatar(), groupViewHolder.headTwo, ImageOptions.optionHead);
        if (!TextUtils.isEmpty(post.getUname())) {
            groupViewHolder.nameTwo.setText(Html.fromHtml(post.getUname()));
        }
        if (!TextUtils.isEmpty(post.getPlain())) {
            groupViewHolder.descTwo.setText(Html.fromHtml(post.getPlain()));
            groupViewHolder.descTwo.setVisibility(View.VISIBLE);
        } else {
            groupViewHolder.descTwo.setVisibility(View.GONE);
        }
        List<Multi> multi = post.getMulti();
        if (null == multi || multi.size() == 0) {
            groupViewHolder.imagesTwo.setVisibility(View.GONE);
        } else {
            groupViewHolder.imagesTwo.setVisibility(View.VISIBLE);
            switch (multi.size()) {
                case 1:
                    imageLoader.displayImage(multi.get(0).getPath(), groupViewHolder.sencondPicOne, ImageOptions.options);
                    groupViewHolder.sencondPicOne.setVisibility(View.VISIBLE);
                    groupViewHolder.sencondPicTwo.setVisibility(View.INVISIBLE);
                    groupViewHolder.sencondPicThree.setVisibility(View.INVISIBLE);
                    break;
                case 2:
                    imageLoader.displayImage(multi.get(0).getPath(), groupViewHolder.sencondPicOne, ImageOptions.options);
                    imageLoader.displayImage(multi.get(1).getPath(), groupViewHolder.sencondPicTwo, ImageOptions.options);
                    groupViewHolder.sencondPicOne.setVisibility(View.VISIBLE);
                    groupViewHolder.sencondPicTwo.setVisibility(View.VISIBLE);
                    groupViewHolder.sencondPicThree.setVisibility(View.INVISIBLE);
                    break;
                default:
                    imageLoader.displayImage(multi.get(0).getPath(), groupViewHolder.sencondPicOne, ImageOptions.options);
                    imageLoader.displayImage(multi.get(1).getPath(), groupViewHolder.sencondPicTwo, ImageOptions.options);
                    imageLoader.displayImage(multi.get(2).getPath(), groupViewHolder.sencondPicThree, ImageOptions.options);
                    groupViewHolder.sencondPicOne.setVisibility(View.VISIBLE);
                    groupViewHolder.sencondPicTwo.setVisibility(View.VISIBLE);
                    groupViewHolder.sencondPicThree.setVisibility(View.VISIBLE);
                    break;
            }
        }
        if (!TextUtils.isEmpty(post.getUid())) {
            View.OnClickListener onClick = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, post.getUid());
                }
            };
            groupViewHolder.headTwo.setOnClickListener(onClick);
            groupViewHolder.nameTwo.setOnClickListener(onClick);
        }
        if (go_detail) {
            if (!TextUtils.isEmpty(post.getPost_id())) {
                View.OnClickListener onClick = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, PostDetailActivity.class);
                        intent.putExtra(MyConstants.POSTIDVALUE, post.getPost_id());
                        intent.putExtra(MyConstants.GIDVALUE, groupWork.getGid());
                        intent.putExtra(MyConstants.TIMELINETYPE, TimelineFactory.TIMELINE_TYPE_GROUP);
                        mContext.startActivity(intent);
                    }
                };
                groupViewHolder.descTwo.setOnClickListener(onClick);
                groupViewHolder.imagesTwo.setOnClickListener(onClick);
            }
        }
    }

    public void setLoading(boolean loading) {
        this.loading = loading;
    }

    public void setIs_end(boolean is_end) {
        this.is_end = is_end;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setCount(String count) {
        this.count = count;
    }
}
